package com.yonyou.sto.modules.ums.controller;


import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.sto.common.api.CommonPage;
import com.yonyou.sto.common.api.CommonResult;
import com.yonyou.sto.common.domain.DownloadFile;
import com.yonyou.sto.modules.ims.dto.TaxItemDto;
import com.yonyou.sto.modules.ims.model.ImsParty;
import com.yonyou.sto.modules.ums.dto.UmsNcCusttaxParam;
import com.yonyou.sto.modules.ums.model.UmsAdmin;
import com.yonyou.sto.modules.ums.model.UmsNcCustomer;
import com.yonyou.sto.modules.ums.model.UmsNcCusttax;
import com.yonyou.sto.modules.ums.service.UmsAdminService;
import com.yonyou.sto.modules.ums.service.UmsNcCustomerService;
import com.yonyou.sto.modules.ums.service.UmsNcCusttaxService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aegon
 * @since 2022-11-25
 */
@RestController
@RequestMapping("/server/ums/umsNcCusttax")
public class UmsNcCusttaxController {

    @Autowired
    private UmsNcCusttaxService umsNcCusttaxService;

    @Autowired
    private UmsAdminService adminService;

    @ApiOperation(value = "根据付款单位分页查询客户信息")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<UmsNcCusttax>> list(@RequestParam(value = "def1", required = false) String def1,
                                                       @RequestParam(value = "def3", required = false) String def3,
                                                       @RequestParam(value = "def4", required = false) String def4,
                                                       @RequestParam(value = "taxcode", required = false) String taxcode,
                                                       @RequestParam(value = "pageSize", defaultValue = "100") Integer pageSize,
                                                       @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<UmsNcCusttax> partyPage = umsNcCusttaxService.list(def1,def3,def4,taxcode, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(partyPage));
    }

    @ApiOperation(value = "客户对照关系详情")
    @GetMapping(value = "/info/{id}")
    public CommonResult<UmsNcCusttax> info(@PathVariable String id) {
        return umsNcCusttaxService.info(id);
    }

    @ApiOperation(value = "新增客户对照关系信息")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult create(@Validated @RequestBody UmsNcCusttax umsNcCusttax) {
        return umsNcCusttaxService.create(umsNcCusttax);
    }

    @ApiOperation(value = "修改客户对照关系")
    @RequestMapping(value = "/update/{pkCusttax}", method = RequestMethod.POST)
    public CommonResult update(@PathVariable String pkCusttax, @RequestBody UmsNcCusttax umsNcCusttax) {
//        UmsNcCusttax byId = umsNcCusttaxService.getById(id);
//        if (StrUtil.isNotBlank(umsNcCusttax.getVerifyUsername())) {
//            UmsAdmin umsAdmin = adminService.getAdminByUsername(umsNcCusttax.getVerifyUsername());
//            umsNcCusttax.setVerifyNickname(umsAdmin.getNickName());
//        }
//        imsParty.setId(id);
//        boolean success = umsNcCusttaxService.updateById(u);
//        if (success) {
//            return CommonResult.success("success");
//        }
        return umsNcCusttaxService.update(pkCusttax,umsNcCusttax);
    }

    @ApiOperation(value = "下载导入模板")
    @GetMapping(value = "/downloadFile")
    public void downloadFile(HttpServletRequest request, HttpServletResponse response){
        try {
            InputStream is = this.getClass().getClassLoader().getResourceAsStream("ncCusttaxTemplate.xlsx");
            byte[] fileData = DownloadFile.input2byte(is);
            DownloadFile.downloadFile(response, request, "ncCusttaxTemplate.xlsx", fileData);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

    @ApiOperation("删除指定用户信息")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestParam String ids) {
        List<String> list = CollectionUtils.arrayToList(ids.split(","));
        boolean success = umsNcCusttaxService.delUmsNcCusttax(list);
        //boolean success= umsNcCusttaxService.removeByIds(a);
        if (success) {
            return CommonResult.success("删除对照关系成功");
        }
        return CommonResult.failed("删除对照关系失败");
    }

    @ApiOperation(value = "组织导入excel")
    @RequestMapping(value = "/importNcCusttax")
    public CommonResult importNcCusttax(MultipartFile file) {
        return umsNcCusttaxService.importNcCusttax(file);
    }
}

